#include "section_header_widget.h"
#include <QHBoxLayout>
#include <QAbstractItemView>
#include <QDebug>
#include "section/symtab_widget.h"
#include "section/dynamic_widget.h"
SectionHeaderWidget::SectionHeaderWidget(QWidget *parent)
    : QWidget{parent}
    , tableWidget(new QTableWidget(this))
    , elfRead(nullptr)
{
    intiUI();
    initConnect();
}

void SectionHeaderWidget::updateData( ELFRead *elfRead)
{
    m_elfSectionHeaders  =  elfRead->ReadFileSectionHeader();
    this->elfRead  = elfRead;
    tableWidget->clear();
    tableWidget->setColumnCount(11);
    tableWidget->setRowCount(m_elfSectionHeaders.size());
    for(int i=0 ;i < m_elfSectionHeaders.size() ;i ++){
        QTableWidgetItem *itemId = new QTableWidgetItem(QString::number(i));
        QTableWidgetItem *itemName = new QTableWidgetItem(QString(m_elfSectionHeaders[i].strSectionName.c_str()));
        QTableWidgetItem *itemType = new QTableWidgetItem(QString(m_elfSectionHeaders[i].strSectionType.c_str()));
        QTableWidgetItem *itemFlags = new QTableWidgetItem(QString(m_elfSectionHeaders[i].strSectionFlags.c_str()));
        QTableWidgetItem *itemVirtualAddr = new QTableWidgetItem(QString(m_elfSectionHeaders[i].strSectionVirtualAddr.c_str()));
        QTableWidgetItem *itemOffset = new QTableWidgetItem(QString(m_elfSectionHeaders[i].strSectionOffset.c_str()));
        QTableWidgetItem *itemSize = new QTableWidgetItem(QString(m_elfSectionHeaders[i].strSectionSize.c_str()));
        QTableWidgetItem *itemLink = new QTableWidgetItem(QString(m_elfSectionHeaders[i].strSectionLink.c_str()));
        QTableWidgetItem *itemInfo = new QTableWidgetItem(QString(m_elfSectionHeaders[i].strSectionInfo.c_str()));
        QTableWidgetItem *itemAlignment = new QTableWidgetItem(QString(m_elfSectionHeaders[i].strSectionAlignment.c_str()));
        QTableWidgetItem *itemEntrySize = new QTableWidgetItem(QString(m_elfSectionHeaders[i].strSectionEntrySize.c_str()));

        itemId->setFlags(itemId->flags() & ~Qt::ItemIsEditable);
        itemName->setFlags(itemName->flags() & ~Qt::ItemIsEditable);
        itemType->setFlags(itemType->flags() & ~Qt::ItemIsEditable);
        itemFlags->setFlags(itemFlags->flags() & ~Qt::ItemIsEditable);
        itemVirtualAddr->setFlags(itemVirtualAddr->flags() & ~Qt::ItemIsEditable);
        itemOffset->setFlags(itemOffset->flags() & ~Qt::ItemIsEditable);
        itemSize->setFlags(itemSize->flags() & ~Qt::ItemIsEditable);
        itemLink->setFlags(itemLink->flags() & ~Qt::ItemIsEditable);
        itemInfo->setFlags(itemInfo->flags() & ~Qt::ItemIsEditable);
        itemAlignment->setFlags(itemAlignment->flags() & ~Qt::ItemIsEditable);
        itemEntrySize->setFlags(itemEntrySize->flags() & ~Qt::ItemIsEditable);

        tableWidget->setItem(i, 0, itemId);
        tableWidget->setItem(i, 1, itemName);
        tableWidget->setItem(i, 2, itemType);
        tableWidget->setItem(i, 3, itemFlags);
        tableWidget->setItem(i, 4, itemVirtualAddr);
        tableWidget->setItem(i, 5, itemOffset);
        tableWidget->setItem(i, 6, itemSize);
        tableWidget->setItem(i, 7, itemLink);
        tableWidget->setItem(i, 8, itemInfo);
        tableWidget->setItem(i, 9, itemAlignment);
        tableWidget->setItem(i, 10, itemEntrySize);
    }
}


void SectionHeaderWidget::intiUI()
{
    QHBoxLayout *mainLayout = new QHBoxLayout();
    mainLayout->addWidget(tableWidget);
    tableWidget->setSelectionBehavior(QTableWidget::SelectRows);
    setLayout(mainLayout);
}

void SectionHeaderWidget::initConnect()
{

    connect(tableWidget,&QTableWidget::cellDoubleClicked,this,&SectionHeaderWidget::cellDoubleClicked);
}

void SectionHeaderWidget::cellDoubleClicked(int row, int col)
{
    if(m_elfSectionHeaders[row].strSectionName == ".symtab"){
        SymtabWidget *symtabWidget = new SymtabWidget();
        symtabWidget->updataData(elfRead,row);
        symtabWidget->show();

    }else if(m_elfSectionHeaders[row].strSectionName == ".dynamic"){
        DynamicWidget *dynamicWidget = new DynamicWidget();
        dynamicWidget->updataData(elfRead,row);
        dynamicWidget->show();
    }
}
